export function parseUrl(url){
    var json = {};
    var arr = url.substr(url.indexOf('?') + 1).split('&');
   arr.forEach(item=>{
       var tmp = item.split('=');
                json[tmp[0]] = tmp[1];
   });
   return json;
}

export class Reponse {
    constructor(data,msg='成功', code=0){
      this.code = code;
      this.msg  = msg;
      this.data = data;
    }
    
    splitPage(list,pageSize=1,page=1){
      let tem = {
        "total": 0,
        "page": page,
        "pageSize": pageSize,
        "rows":[]
      }
      
      tem.total = list.length;
      let currentTotal = (page-1)*pageSize
      tem.rows = list.splice(currentTotal, pageSize)
      
      this.data = tem
      return this
    }

    filterPage(list, condition, values){
      switch (condition){
        case 'status':
          return list.filter(item => Array.from(values).includes(item[condition]));
          break;
        default: 
          return this;
      }
    }
  
    run(){
      return {
        code: this.code,
        msg:  this.msg,
        data: this.data
      }
    }
  }

export function checkPhone(number){ 
  let _number = number;
  if(!(/^1(3|4|5|7|8)\d{9}$/.test(_number))){ 
      return false; 
  } else {
    return true;
  }
}
import { Message } from 'element-ui';
export function systemMaintenance(msg){
  // this.$message(msg);
  Message('该功能正在开发中');
}

export function myBrowser(){
  var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串

  if (userAgent.indexOf("Chrome") > -1){
    return "Chrome";
  }else{
      // alert(userAgent);
    return '其他'
  } 
}

export class verifyFrom{
  constructor(list, model){
    this.list = list;
    this.model = model;
    this.initialMate();
  }

  async initialMate(){
    let res = [];
    res = res.concat(await this.list.map((val,index)=>{
      return this.analyzeRule(val);
    }))

    res = res.filter(val=>val!==undefined&&val.length>0);
    return res;
  }

  analyzeRule(val){
    let req = []
    if(val.rule == undefined) return;
    val.rule.forEach(einzelband=>{
      switch (einzelband) {
        case 'required':
          if(this.model[val.model].length == 0){
            req = `${val['title']}不能为空`;
          } 
          break;
      
        default:
          break;
      }
      
    })
    return req;
  }
  
}